package pm

import (
	"context"
	"encoding/json"
	"ez/apps/pm/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type House struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64      `json:"id" bson:"id,omitempty"`
	Name        string     `json:"name" bson:"name"`               //楼栋号
	Area        float64    `json:"area" bson:"area"`               //面积
	Community   *Community `json:"community" bson:"community"`     //所属小区
	CommunityId int64      `json:"communityId" bson:"communityId"` //所属小区
	Type        *HouseType `json:"type" bson:"type"`               //房屋类型
	TypeId      int64      `json:"typeId" bson:"typeId"`           //房屋类型
	Owner       []*Owner   `json:"owner" bson:"owner"`             //所属业主
	OwnerIds    []int64    `json:"ownerIds" bson:"ownerIds"`       //所属业主
	Car         []*Car     `json:"car" bson:"car"`                 //所属业主
	CarIds      []int64    `json:"carIds" bson:"carIds"`           //所属业主
	Note        string     `json:"note" bson:"note"`               //备注
	CreateAt    time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt    time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt    *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *House) DocName() string { return "House" }
func (this *House) GetId() int64    { return this.Id }
func (this *House) SetId(id int64)  { this.Id = id }
func (this *House) Create() error {
	return this.GetFactory().Create(this)
}
func (this *House) Replace() error {
	return this.GetFactory().Replace(this)
}
func (this *House) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *House) Delete() error {
	return this.GetFactory().Delete(this)
}
func (this *House) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *House) Destroy() error {
	return this.GetFactory().Destroy(this)
}
func (this *House) ToString() string {
	return string(this.ToBytes())
}
func (this *House) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *House) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *House) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}
func (this *House) LoadCommunity() {
	if this.CommunityId == 0 {
		return
	}
	this.Community, _ = NewCommunityCrud().FindId(this.CommunityId)
}
func (this *House) LoadType() {
	if this.TypeId == 0 {
		return
	}
	this.Type, _ = NewHouseTypeCrud().FindId(this.TypeId)
}
func (this *House) LoadOwner() {
	if this.Owner == nil {
		this.Owner = make([]*Owner, 0)
	}
	if this.OwnerIds == nil {
		this.OwnerIds = make([]int64, 0)
	}
	if len(this.OwnerIds) > 0 {
		this.Owner = make([]*Owner, 0)
		for _, v := range this.OwnerIds {
			find, e := NewOwnerCrud().FindId(v)
			if e == nil {
				this.Owner = append(this.Owner, find)
			}
		}
	}
}
func (this *House) LoadCar() {
	if this.Car == nil {
		this.Car = make([]*Car, 0)
	}
	if this.CarIds == nil {
		this.CarIds = make([]int64, 0)
	}
	if len(this.CarIds) > 0 {
		this.Car = make([]*Car, 0)
		for _, v := range this.CarIds {
			find, e := NewCarCrud().FindId(v)
			if e == nil {
				this.Car = append(this.Car, find)
			}
		}
	}
}
func (this *House) ClearRelationsBeforeSave() mgo.Doc {
	this.Community = nil
	this.Type = nil
	this.Owner = nil
	this.Car = nil
	return this
}
func neverUsed_House() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type HouseAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *House
	Session *ez.Session
}

func NewHouseAccessControl(model *House, action string, session *ez.Session) *HouseAccessControl {
	ctrl := &HouseAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.HouseAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
